package com.ai.file;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import static com.ai.module.chat.util.FileEncodingDetection.getFileCharsetName;

/**
 * @author: MrHe
 * @create: 2023-11-17 21:57
 * @description:
 **/
@Slf4j
public class FileTUtils {

    public static final String BASE_URL="D:\\goodBook\\玄幻";
    public static void main(String[] args) throws Exception {
//        String filePath = "F:\\资源ook\\仙侠小说\\109《仙界修仙》作者：莫默.txt";
//
//        String fileCharsetName = getFileCharsetName(new File(filePath));
//        System.out.println("fileCharsetName = " + fileCharsetName);
//
        File file = new File(BASE_URL);
        File[] files = file.listFiles();
        List<FileContentVo> fileList = new ArrayList<>();
        for (File f : files) {
            Charset fileCharsetName = getFileCharsetName(f);
             bufferReaderTest(f, fileCharsetName,fileList);
        }
        System.out.println("fileList = " + fileList);

    }

    public static void bufferReaderTest(File txtFile, Charset charset, List<FileContentVo> fileList) {
        StringBuilder sb = new StringBuilder();
        int count = 1;
        String fileName = txtFile.getName();
        fileName = txtFile.getName().replace(".text","");
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(txtFile,charset))) {
            String line;
            while ((line = bufferedReader.readLine())  != null) {
                // 处理每一行数据 .....
                sb.append(line);
                String content = sb.toString();
                if (content.length() > 512 * 1024){
                    FileContentVo fileContentVo = new FileContentVo();
                    fileContentVo.setContent(content);
                    fileContentVo.setFileName(fileName);
                    fileContentVo.setCount(count);
                    fileList.add(fileContentVo);
                    sb = new StringBuilder();
                    count++;
                }
            }
            FileContentVo fileContentVo = new FileContentVo();
            fileContentVo.setContent(sb.toString());
            fileContentVo.setFileName(fileName);
            fileContentVo.setCount(count);
            fileList.add(fileContentVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

    }


}
